/*
* Copyright (c) 2007 by Kirill Kolodyazhniy.
* See the file "license.terms" for information on usage and redistribution.
*/

#ifndef M_LFPRIMITIVES_H
#define M_LFPRIMITIVES_H

#include "atomic.h"

#include "lfnode.h"

namespace Engine
{
	namespace LockFree
	{
		typedef unsigned int uint32_t;
		/*************************************************************************************************/
		/**
		*   Compare and swap function.
		*   Algorithm: if(*_ptr == oldVal) *_ptr = newVal .
		*   @return true if new value was assigned to _ptr.
		*/
		template<typename T>
		bool CAS(node<T> * volatile * _ptr, node<T> * oldVal, node<T> * newVal)
		{
			return AtomicCompareExchange(reinterpret_cast<volatile long*>(_ptr),reinterpret_cast<long>(oldVal),reinterpret_cast<long>(newVal));
		}
		/*************************************************************************************************/
		/**
		*   Compare and swap function.
		*   Algorithm: *   Atomically:  if(_ptr[0] == old1 && _ptr[1] == old2){_ptr[0] = new1; _ptr[1] = new2;}.
		*   @return true if new value was assigned to _ptr.
		*/
		template<typename T>
		bool CAS2(node<T> * volatile * _ptr, node<T> * old1, unsigned int old2, node<T> * new1, unsigned int new2)
		{
			return AtomicCompareExchange64(reinterpret_cast<volatile long*>(_ptr),reinterpret_cast<long>(old1),old2,reinterpret_cast<long>(new1),new2);
		}
		/*************************************************************************************************/
	}
}

#endif
